home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / async.zip / FORKP.C < prev    next >
C/C++ Source or Header  |  1986-09-25  |  2KB  |  106 lines

  1. #include <stdio.h>
  2. char *getenv();
  3. #include <ctype.h>
  4. #ifndef NULL
  5. #define NULL ((void *)0)
  6. #endif
  7.  
  8. int forkp(cmdline)
  9. char *cmdline;
  10. {
  11.     register char *cp, *xp;
  12.     int result;
  13.     static char path[64];
  14.  
  15.     if (-1 != (result = tryfork("", cmdline)))
  16.         return result;
  17.     if ((cp = getenv("PATH")) != 0) 
  18.     {
  19.         while (*cp) 
  20.         {
  21.             xp = path;
  22.             while (*cp) 
  23.             {
  24.                 if (*cp == ';') 
  25.                 {
  26.                     ++cp;
  27.                     break;
  28.                 }
  29.                 *xp++ = *cp++;
  30.             }
  31.             *xp = 0;
  32.             if (path[0] != 0)
  33.                 if (-1 != (result = tryfork(path, cmdline)))
  34.                     return result;
  35.         }
  36.     }
  37.     return -1;
  38. }
  39.  
  40. static
  41. int tryfork(dir, cmdline)
  42. char *dir, *cmdline;
  43. {
  44.     static char newname[64];
  45.     static char name[64];
  46.     register char *cp;
  47.     register int i;
  48.     int wait();
  49.     char *strrchr(),*strchr();
  50.     
  51.     /* get the name */
  52.     for (i = 0, cp = cmdline; !isspace(*cp);cp++,i++)
  53.         name[i] = *cp;
  54.     name[i] = '\0';    /* terminate the string */
  55.     
  56.     strcpy(newname, dir);
  57.     if (((cp = strchr(newname, '/')) || (cp = strchr(newname, '\\')))
  58.                 && *(cp+1) != '\0')
  59.         strcat(newname, "/");
  60.     strcat(newname, name);
  61.     if (strchr(name,'.') == NULL)
  62.         strcat(newname,".com");
  63.     if (-1 != fork(newname, cmdline))
  64.         return wait();
  65.     strcpy(strrchr(newname,'.'), ".exe");
  66.     if (-1 != fork(newname, cmdline))
  67.         return wait();
  68.     return -1;
  69. }
  70.  
  71. int fork(path, cmdline)
  72. char *path, *cmdline;
  73. {
  74.     static char cmdbuf[131];
  75.     char *f1 = NULL,*f2 = NULL;
  76.     char fcb1[16], fcb2[16];
  77.     fcbinit(".", fcb1);        /* initialize fcb's, in case no args */
  78.     fcbinit(".", fcb2);
  79.     /* skip the zeroeth arg on the command line */
  80.     while(*cmdline && isspace(*cmdline))    /* skip blanks if any */
  81.         ++cmdline;
  82.     while(*cmdline && !isspace(*cmdline))    /* skip non-blanks */
  83.         ++cmdline;
  84.     for(f1 = cmdline;*f1 && isspace(*f1);)    /* find first non blank for f1 */
  85.         ++f1;
  86.     for(f2 = f1;*f2 && !isspace(*f2);)        /* find first blank for f2 */
  87.         ++f2;
  88.     while (*f2 && isspace(*f2))                /* find first non blank for f2 */
  89.         ++f2;
  90.     /* f1 and f2 will be NULL if there is no argv[1] and argv[2] respectively */
  91.     if (*f1)    
  92.         fcbinit(f1,fcb1);
  93.     if (*f2)
  94.         fcbinit(f2,fcb2);
  95.     /* make sure the command line is terminated by a carriage return */
  96.     strncpy(&cmdbuf[1],cmdline,127);
  97.     if (strlen(cmdline) >= 127)
  98.         cmdbuf[128] = '\0';
  99.     cmdbuf[0] = strlen(&cmdbuf[1]);
  100.     strcat(&cmdbuf[1],"\r");
  101. #ifdef DEBUG
  102.     fprintf(stderr,"path = |%s| tail = %s\n",path,&cmdbuf[1]);
  103. #endif
  104.     return fexec(path, NULL, cmdbuf, fcb1, fcb2);
  105. }
  106.